<template>
	<view>
		<!-- <view v-for="(item, index) in questions">
			<view class="item">
				问题 {{item.questionName}}
			</view>
		</view> -->

		<!-- 组件方式实现 -->
		<QuestionnaireList ref="questionnaire" :list='questions' @answer="answerFn" />

		<!-- <menu-test :list="list" /> -->
		<view class="blank"></view>
		<view class="normal-btn" @click="processQues('null')">提交问卷</view>
		<view class="blank"></view>
		<view class="blank"></view>

	</view>
</template>

<script>
	import request from "@/utils/request.js"
	import QuestionnaireList from "@/components/questionnaire/QuestionnaireList.vue"
	import menuTest from "@/components/menu-test.vue"
	export default {
		data() {
			return {
				questions: [],
				answerList: [],
				questionAnswerList: []
			}
		},
		onLoad() {
			this.init()
		},
		components: {
			QuestionnaireList,
			menuTest
		},
		methods: {
			answerFn(item) {
				//方案2收集答案
				let qList = this.answerList
				let index = qList.findIndex(obj => obj.id === item.id)
				index === -1 ? qList.push(item) : qList[index] = item
				this.answerList = qList
				console.log("this.answerList", this.answerList)
			},
			async init() {
				let res = await request.post({
					service: "smarthos.zuobiao.cancer.digestive.tract.questionnaire.list"
				})
				console.log("res", res)
				this.questions = res.obj.questions
			},

			/**
			 * 处理数据
			 * 2024.07.26
			 */
			processQues(dataList) {
				let list = []
				if (dataList === 'null') {
					list = this.questions
				} else {
					list = dataList
				}

				let questionAnswerList = this.answerList.map(item => {
					return {
						questionId: item.id,
						optionValue: item.fValue
					}
				})
				this.questionAnswerList = questionAnswerList

				let verifyPass = true
				for (let item of list) {
					if (item.subQuestions && item.subQuestions.length > 0) {
						this.processQues(item.subQuestions)
					} else {
						let quesItem = questionAnswerList.find(ele => ele.questionId === item.id)
						console.log("quesItem", quesItem);
						if (!quesItem) {
							uni.showToast({
								title: `${item.questionNumber}、${item.questionName}不能为空`,
								icon: "none"
							})
							verifyPass = false
							break;
						}

						if (item.type === 'text_int') {
							if (!/^\d+$/.test(quesItem.optionValue)) {
								uni.showToast({
									title: `${item.questionName}只能是数字`,
									icon: "none"
								})
								verifyPass = false
								break;
							}
						}

						if (item.identifyKey === 'phone') {
							if (!/^1[3-9]\d{9}$/.test(quesItem.optionValue)) {
								uni.showToast({
									title: `手机号码格式错误`,
									icon: "none"
								})
								verifyPass = false
								break;
							}
						}
					}
				}

				//提交数据
				verifyPass && this.submitFn()
			},
			/**
			 * 提交
			 */
			async submitFn() {
				let res = await request.post({
					service: "smarthos.zuobiao.cancer.digestive.tract.questionnaire.answer.save",
					patId: "651362202624",
					patSex: "M",
					age: 33,
					questionAnswerList: this.questionAnswerList
				})
				
				if(res.code==='0'){
					// uni.setStorageSync("resData",res.obj)
					let ress={"code":"0","msg":"提交问卷成功!","obj":{"age":33,"answerContent":"","bmiValue":21.604938271604937,"changeIsBloodyStool":"是","changeIsChronicDiarrhea":"否","changeIsConstipationHistory":"偶尔","changeIsDrink":"偶尔","changeIsFollowUp":"","changeIsMentalInjury":"偶尔","changeIsSmoke":"从不","changePatSex":"男","colonCancerResult":"1","colonCancerSuggest":"您的生活饮食习惯良好，自律性强，建议继续保持，日常多运动，多吃蔬菜水果，并带动家人和朋友一起来做，建议3年复查一次结肠镜以监测胃肠健康情况，若有不适及时到医院就诊。","createTime":1722231540557,"creatorId":"0","creatorType":"SYS","esophagealCancerResult":"2","esophagealCancerSuggest":"您有多项食管癌的潜在危险因素，建议改善生活习惯，均衡饮食，增加运动，定期进行食管镜检查，以监测食管健康状况。","gastricCancerResult":"2","gastricCancerSuggest":"经排查，您现阶段有多项胃癌危险因素，处于胃癌患病中度危险，建议改变饮食习惯，调节饮食结构，多运动，多吃蔬菜水果，并定期到医院行胃镜、食管镜检查以明确诊断。","height":"180","hosId":"1400000000","hosName":"北京京顺医院","id":"816989553664","isBloodyStool":"3","isCancerPat":"0","isChronicDiarrhea":"1","isConstipationHistory":"2","isDrink":"2","isFollowUp":"0","isMentalInjury":"2","isSmoke":"1","patId":"651362202624","patName":"杨文斌","patSex":"M","phone":"19920050123","questionJson":"","questionnaireName":"消化道癌症风险评估","weight":"70"},"succ":true}
					uni.setStorageSync("resData",ress.obj)
					uni.navigateTo({
						url: '/pages/wenjuanxq'
					})
				}
				console.log("fdsafdsf", res)
			},
		}
	}
</script>

<style>

</style>